package com.androidviewx.badge;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.androidviewx.R;
import com.androidviewx.badge.adapter2.AppAdapter;
import com.androidviewx.badge.adapter2.BaseAdapter;
import com.xfunc.views.badge.BGABadgeFloatingActionButton;
import com.xfunc.views.badge.BGABadgeFrameLayout;
import com.xfunc.views.badge.BGABadgeImageView;
import com.xfunc.views.badge.BGABadgeLinearLayout;
import com.xfunc.views.badge.BGABadgeRadioButton;
import com.xfunc.views.badge.BGABadgeRelativeLayout;
import com.xfunc.views.badge.BGABadgeTextView;
import com.xfunc.views.badge.BGABadgeView;
import com.xfunc.views.badge.core.BGABadgeable;
import com.xfunc.views.badge.core.BGADragDismissDelegate;

public class DemoBadgeViewActivity extends AppCompatActivity {
    private BGABadgeView mTestBv ;
    private BGABadgeTextView mTestBtv ;
    private BGABadgeImageView mNormalBiv ;
    private BGABadgeImageView mRoundedBiv ;
    private BGABadgeImageView mCircleBiv ;

    private BGABadgeLinearLayout mTestBll ;
    private BGABadgeRelativeLayout mTestBrl ;
    private BGABadgeFrameLayout mTestBfl ;

    private RadioGroup mTabRg ;
    private BGABadgeRadioButton mHomeBrb ;
    private BGABadgeRadioButton mMessageBrb ;
    private BGABadgeRadioButton mDiscoverBrb ;
    private BGABadgeRadioButton mMeBrb ;
    private BGABadgeFloatingActionButton mChatBfab ;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_badge_layout);
        initView();
        testBadgeView();
        testRadioButton();
        testRecyclerView();
    }
    private void initView() {
        mTestBv = findViewById(R.id.bv_main_test);
        mTestBtv = findViewById(R.id.btv_main_test);
        mNormalBiv = findViewById(R.id.biv_main_normal);
        mRoundedBiv = findViewById(R.id.biv_main_rounded);
        mCircleBiv = findViewById(R.id.biv_main_circle);
        mTestBll = findViewById(R.id.bll_main_test);
        mTestBrl = findViewById(R.id.brl_main_test);
        mTestBfl = findViewById(R.id.bfl_main_test);
        mTabRg = findViewById(R.id.rg_main_tab);
        mHomeBrb = findViewById(R.id.brb_main_home);
        mMessageBrb = findViewById(R.id.brb_main_message);
        mDiscoverBrb = findViewById(R.id.brb_main_discover);
        mMeBrb = findViewById(R.id.brb_main_me);
        mChatBfab = findViewById(R.id.bfab_main_chat);
    }
    private void testBadgeView() {
        mTestBv.showTextBadge("9");
        mTestBv.getBadgeViewHelper().setBadgeTextSizeSp(15);
        mTestBv.getBadgeViewHelper().setBadgePaddingDp(8);
        mTestBv.getBadgeViewHelper().setBadgeTextColorInt(Color.parseColor("#FF0000"));
        mTestBv.getBadgeViewHelper().setBadgeBgColorInt(Color.parseColor("#00FF00"));
        mTestBv.getBadgeViewHelper().setDraggable(true);
        mTestBv.getBadgeViewHelper().setBadgePaddingDp(7);
        mTestBv.getBadgeViewHelper().setBadgeBorderWidthDp(2);
        mTestBv.getBadgeViewHelper().setBadgeBorderColorInt(Color.parseColor("#0000FF"));
        mTestBtv.showCirclePointBadge();
        mNormalBiv.showCirclePointBadge();
        Bitmap avatarBadgeBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.badge_avatar_vip);
        RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.mipmap.badge_avator));
        roundedDrawable.getPaint().setAntiAlias(true);
        roundedDrawable.setCornerRadius(30f);
        mRoundedBiv.setImageDrawable(roundedDrawable);
        mRoundedBiv.showDrawableBadge(avatarBadgeBitmap);
        Bitmap avatarBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.badge_avator);
        RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), avatarBitmap);
        circleDrawable.getPaint().setAntiAlias(true);
        circleDrawable.setCornerRadius(Math.max(avatarBitmap.getWidth(), avatarBitmap.getHeight()) / 2.0f);
        mCircleBiv.setImageDrawable(circleDrawable);
        mCircleBiv.showDrawableBadge(avatarBadgeBitmap);
        mTestBll.showDrawableBadge(avatarBadgeBitmap);
        mTestBrl.showTextBadge("LoveAndroid");
        mTestBfl.showTextBadge("8");
        mChatBfab.showTextBadge("8");
        mChatBfab.setDragDismissDelegate(badgeable -> showToast("清空聊天消息"));
        new Handler().postDelayed(() -> mRoundedBiv.hiddenBadge(), 3000);
        new Handler().postDelayed(() -> mRoundedBiv.showCirclePointBadge(), 6000);
        new Handler().postDelayed(() -> mRoundedBiv.showDrawableBadge(
                BitmapFactory.decodeResource(
                        getResources(),
                        R.mipmap.badge_avatar_vip
                )
        ), 9000);
    }

    private void testRadioButton() {
        mHomeBrb.showTextBadge("10");
        mMessageBrb.showTextBadge("1");
        mDiscoverBrb.showTextBadge("...");
        mMeBrb.showDrawableBadge(BitmapFactory.decodeResource(getResources(), R.mipmap.badge_avatar_vip));
        new Handler().postDelayed(() -> mHomeBrb.showTextBadge("1"), 5000);
        mHomeBrb.setDragDismissDelegate(badgeable -> showToast("消息单选按钮徽章拖动消失"));
        mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case  R.id.brb_main_home: showToast("首页");
                    case R.id.brb_main_message :  showToast("消息");
                    case  R.id.brb_main_discover :  showToast("发现");
                    case R.id.brb_main_me: showToast("我");
                }
            }
        });
    }
    void testRecyclerView(){
        RecyclerView recyclerview = findViewById(R.id.recyclerview);
        AppAdapter adapter2 = new AppAdapter<MessageModel>(this) {
            @NonNull
            @Override
            public BaseAdapter<?>.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new ViewHolder(R.layout.badge_item_message){
                    @Override
                    public void onBindView(int position) {
                        MessageModel item = getItem(position);
                        TextView t1 = findViewById(R.id.tv_item_message_title);
                        TextView t2 = findViewById(R.id.tv_item_message_detail);
                        t1.setText(item.getTitle());
                        t2.setText(item.getDetail());
                        BGABadgeLinearLayout rootView = (BGABadgeLinearLayout) getItemView();
                        if (item.getNewMsgCount() > 0) {
                            rootView.showTextBadge("" + item.getNewMsgCount());
                            rootView.setDragDismissDelegate(new BGADragDismissDelegate() {
                                @Override
                                public void onDismiss(BGABadgeable badgeable) {
                                    item.setNewMsgCount(0);
                                    showToast(item.getTitle() + "的徽章消失");
                                }
                            });
                        } else {
                            rootView.hiddenBadge();
                        }
                    }
                };
            }
        };
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter2.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                showToast("点击了条目 " + ((MessageModel)adapter2.getItem(position)).getTitle());
            }
        });
        recyclerview.setAdapter(adapter2);
        adapter2.addData(MessageModel.getTestData());
    }
    public void showToast(String message){
        AppToast.show(this,message);
    }
}
